package com.googlecode.adaboost.module;

import java.util.Map;

import com.googlecode.adaboost.exception.AdaBoostException;

public class ModuleUtil {

	/** This is a utility class, so no instances allowed. */
	private ModuleUtil() {/**/
	}

	@SuppressWarnings("rawtypes")
	public static void missingParameterError(Class cls, String parameterName)
			throws AdaBoostException {
		throw new AdaBoostException(cls.getName() + " requires the parameter '"
				+ parameterName + "' to be set (see configuration).");
	}

	@SuppressWarnings("rawtypes")
	public static int getIntegerParameter(Class cls, String parameterName,
			Map<String, String> settings) throws AdaBoostException {
		if (settings.containsKey(parameterName) == false)
			ModuleUtil.missingParameterError(cls, parameterName);
		return Integer.parseInt(settings.get(parameterName).trim());
	}

	@SuppressWarnings("rawtypes")
	public static int getOptionalIntegerParameter(Class cls,
			String parameterName, Map<String, String> settings, int defaultValue) {
		if (settings.containsKey(parameterName) == false)
			return defaultValue;
		return Integer.parseInt(settings.get(parameterName).trim());
	}

	@SuppressWarnings({ "rawtypes" })
	public static double getDoubleParameter(Class cls, String parameterName,
			Map<String, String> settings) throws AdaBoostException {
		if (settings.containsKey(parameterName) == false)
			ModuleUtil.missingParameterError(cls, parameterName);
		return Double.parseDouble(settings.get(parameterName).trim());
	}

	@SuppressWarnings({ "rawtypes" })
	public static double getOptionalDoubleParameter(Class cls,
			String parameterName, Map<String, String> settings,
			double defaultValue) {
		if (settings.containsKey(parameterName) == false)
			return defaultValue;
		return Double.parseDouble(settings.get(parameterName).trim());
	}

	@SuppressWarnings({ "rawtypes" })
	public static SettingsReader getSettingsReader(Class cls,
			Map<String, String> settings) {
		return new SettingsReader(cls, settings);
	}

	public static class SettingsReader {
		@SuppressWarnings("rawtypes")
		Class cls;
		Map<String, String> settings;

		@SuppressWarnings({ "rawtypes" })
		SettingsReader(Class cls, Map<String, String> settings) {
			this.cls = cls;
			this.settings = settings;
		}

		public int getInteger(String parameterName) throws AdaBoostException {
			return ModuleUtil.getIntegerParameter(cls, parameterName, settings);
		}

		public int getOptionalInteger(String parameterName, int defaultValue) {
			return ModuleUtil.getOptionalIntegerParameter(cls, parameterName,
					settings, defaultValue);
		}

		public double getDouble(String parameterName) throws AdaBoostException {
			return ModuleUtil.getDoubleParameter(cls, parameterName, settings);
		}

		public double getOptionalDouble(String parameterName,
				double defaultValue) {
			return ModuleUtil.getOptionalDoubleParameter(cls, parameterName,
					settings, defaultValue);
		}

	}

}
